package com.imay.live.activity.family.controller;

import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.LinearLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.imay.live.R;
import com.imay.live.activity.dynamics.SearchActivity;
import com.imay.live.activity.family.FamilyCreateActivity;
import com.imay.live.activity.family.FamilyHomeActivity;
import com.imay.live.activity.family.helper.NimLogin;
import com.imay.live.adapter.family.FamilySuggestAdapter;
import com.imay.live.model.FamilyModel;
import com.imay.live.utils.ViewUtil;
import com.imay.live.view.AutoAdjustRecylerView;
import com.yqritc.recyclerviewflexibledivider.VerticalDividerItemDecoration;

import java.util.List;

/**
 * Created by chan on 2017/8/29 0029.
 */

public class FamilyController extends BaseFamilyController implements View.OnClickListener {

    private AutoAdjustRecylerView rl_family_suggest;
    private FamilySuggestAdapter mAdapter;
    private View rl_suggest;

    @Override
    protected void initView() {
        ViewUtil.findView(activity, R.id.ll_create_family).setOnClickListener(this);
        ViewUtil.findView(activity, R.id.ll_apply_family).setOnClickListener(this);
        rl_suggest = ViewUtil.findView(activity, R.id.rl_suggest);
        rl_family_suggest = ViewUtil.findView(activity, R.id.rl_family_suggest);

        initAdapter();
    }

    private void initAdapter() {
        mAdapter = new FamilySuggestAdapter();

        rl_family_suggest.setNestedScrollingEnabled(false);
        LinearLayoutManager labelsLayoutManager = new LinearLayoutManager(activity);
        labelsLayoutManager.setOrientation(LinearLayout.HORIZONTAL);
        rl_family_suggest.setLayoutManager(labelsLayoutManager);
        rl_family_suggest.setPxPerMillsec(1.0f);
        rl_family_suggest.setAdapter(mAdapter);

        rl_family_suggest.addItemDecoration(new VerticalDividerItemDecoration.Builder(activity)
                .colorResId(R.color.bg_f5)
                .sizeResId(R.dimen.size_1dp)
                .build());
        rl_family_suggest.setAdapter(mAdapter);

        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                FamilyModel familyModel = mAdapter.getItem(position);
                FamilyHomeActivity.startActivity(activity, FamilyHomeActivity.OTHERS, familyModel);
            }
        });
    }

    public void setNewData(List<FamilyModel> familys) {
        if (familys.isEmpty()) {
            rl_suggest.setVisibility(View.GONE);
        } else {
            rl_suggest.setVisibility(View.VISIBLE);
            mAdapter.setNewData(familys);
            mAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_create_family:
                FamilyCreateActivity.startActivity(activity, FamilyCreateActivity.CREATE);
                break;
            case R.id.ll_apply_family:
                SearchActivity.startActivity(activity, SearchActivity.FAMILY);
                break;
        }
    }
}
